<?php

class Core_Backend_Component_Foundation_Controller_Poi extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20) {
        $this->getHistory()->record();
        $validator = $this->getRequest()->getValidator('filter');
        $collection = App::getModel('foundation/collection_poi')
                        ->pagination($pageindex, $pagesize);
        if(!$this->getIdentity()->isInRole('resource_manager')){
        	$collection->innerJoin('{t}account_poi', 'z', 'z.poi_id', 'a.id')
        			   ->eq('z.account_id', $this->getIdentity()->getId());
        }
        if ($validator->isNotEmpty('shopname')) {
            $collection->filterByShopName($validator->getValue('shopname'));
        }
        if ($validator->isNotEmpty('poi')) {
            $collection->filterByPoi($validator->getValue('poi'));
        }
        if ($validator->isNotEmpty('industryname')) {
            $collection->filterByIndustryName($validator->getValue('industryname'));
        }
//        if ($validator->isNotEmpty('townstreetname')) {
//            $collection->filterByTownstreetName($validator->getValue('townstreetname'));
//        }
        if ($validator->isNotEmpty('roadid')) {
            $collection->filterByRoadId($validator->getValue('roadid'));
        }
        if ($validator->isNotEmpty('industryid')) {
            $collection->filterByIndustryId($validator->getValue('industryid'));
        }
//        if ($validator->isNotEmpty('roadname')) {
//            $collection->filterByRoadName($validator->getValue('roadname'));
//        }
//        if ($validator->isNotEmpty('districtname')) {
//            $collection->filterByDistrictName($validator->getValue('districtname'));
//        }
        $collection->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records));
    }

    public function assignPostAction() {
        $pois = App::getModel('foundation/collection_poi')
                        ->contain('a.id', ':poiIds:s')
                        ->addParameter('poiIds', $this->getRequest()->getForm('ids'))
                        ->load();
        $accounts = App::getModel('permission/collection_account')->load();
        $selectedAccounts = Kd_Data_Factory::getQueryDcl()
                        ->from('{t}foundation_poi_account')
                        ->contain('poi_id', ':poiIds:s')
                        ->addParameter('poiIds', $this->getRequest()->getForm('ids'))
                        ->field('account_id')
                        ->distinct()
                        ->fetch()
                        ->getFieldValues('account_id');
        return $this->viewResult(array('accounts' => $accounts, 'account_ids' => $selectedAccounts, 'pois' => $pois));
    }

    public function saveAssignPostAction() {
        $poiIds = $this->getRequest()->getForm('poi_ids');
        $accountIds = $this->getRequest()->getForm('account_ids');
        Kd_Data_Factory::getDeleteDcl()
                ->from('{t}foundation_poi_account')
                ->where()->contain('poi_id', ':poiIds:s')->closest()
                ->addParameter('poiIds', $poiIds)
                ->execute();

        $sql = Kd_Data_Factory::getInsertDcl()
                        ->into('{t}foundation_poi_account')
                        ->value('poi_id', ':poiId:s')
                        ->value('account_id', ':accountId:i')
                        ->toSql();
        $driver = Kd_Db_Factory::getDriver();
        foreach ($accountIds as $accountId) {
            foreach ($poiIds as $poiId) {
                $driver->setSql($sql, array('poiId' => $poiId, 'accountId' => $accountId))->execute();
            }
        }
        return $this->redirectResult($this->getHistoryUrl('foundation/poi/index'));
    }

}